import api from '../../api'

export default {
    namespaced: true,
    state: {
        detail: {},
        commentList: [],
        articleList: [],
    },
    mutations: {
        set_detail(state, payload) {
            state.detail = payload;
        },
        set_commentList(state, payload) {
            state.commentList = payload;
        },
        set_articleList(state, payload) {
            state.articleList = payload;
        },
        // 回复组件的显示隐藏
        set_reply(state, payload) {
            let {
                id,
                childId
            } = payload;
            let obj = state.commentList.find((item) => item.id === id);
            if (childId) {
                obj = obj.children.find((item) => item.id === childId);
            }
            if (obj.reply === 0) {
                obj.reply = 1
            } else {
                obj.reply = 0
            }
        },
    },
    actions: {
        // 获取详情
        getPageDetail(context, data) {
            api.getPageDetail(data).then(res => {
                if (res.success) {
                    context.commit('set_detail', res.data)
                }
            })
        },
        // 获取一级评论列表
        getCommentList(context, data) {
            api.getFirstList(data).then((res) => {
                if (res.success) {
                    context.commit("set_commentList", res.data)
                }
            });
        },
        // 获取前六条文章列表
        getArticleList(context,data){
            api.getArticleList(data).then(res=>{
                if(res.success){
                    context.commit("set_articleList",res.data.data);
                }
            })
        }
    },
}